{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "UnboundLocalError",
     "evalue": "local variable 'action' referenced before assignment",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mUnboundLocalError\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-1-c3f9db436db3>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m     55\u001b[0m     \u001b[1;32mreturn\u001b[0m \u001b[0mbrowser\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     56\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 57\u001b[1;33m \u001b[0mlogin\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     58\u001b[0m \u001b[0ma\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     59\u001b[0m \u001b[1;32mwhile\u001b[0m \u001b[0ma\u001b[0m \u001b[1;33m<=\u001b[0m \u001b[1;36m10\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m<ipython-input-1-c3f9db436db3>\u001b[0m in \u001b[0;36mlogin\u001b[1;34m()\u001b[0m\n\u001b[0;32m     43\u001b[0m     \u001b[0mbrowser\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfind_element_by_xpath\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'//input[@name=\"TPL_username\"]'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mclick\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     44\u001b[0m     \u001b[1;31m# 滑块操作\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 45\u001b[1;33m     \u001b[0mslider\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     46\u001b[0m     \u001b[0mbrowser\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfind_element_by_id\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'J_SubmitStatic'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mclick\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     47\u001b[0m     \u001b[0msleep\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m<ipython-input-1-c3f9db436db3>\u001b[0m in \u001b[0;36mslider\u001b[1;34m()\u001b[0m\n\u001b[0;32m     15\u001b[0m     \u001b[0me_nc\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mbrowser\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfind_element_by_id\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'nc_1_n1z'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     16\u001b[0m     \u001b[1;31m# 点击滑块,并且hold住\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 17\u001b[1;33m     \u001b[0maction\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mclick_and_hold\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mon_element\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0me_nc\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mperform\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     18\u001b[0m     \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     19\u001b[0m         \u001b[0mx\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m5\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mUnboundLocalError\u001b[0m: local variable 'action' referenced before assignment"
     ]
    }
   ],
   "source": [
    "from selenium import webdriver\n",
    "from time import sleep\n",
    "from selenium.webdriver.common.action_chains import ActionChains\n",
    "from selenium.webdriver.common.keys import Keys\n",
    "\n",
    "# browser = webdriver.Chrome()\n",
    "browser = webdriver.Firefox()\n",
    "action = ActionChains(browser)\n",
    "def slider():\n",
    "    '''\n",
    "    Fucntion doc\n",
    "    滑块\n",
    "    '''\n",
    "    \n",
    "    e_nc = browser.find_element_by_id('nc_1_n1z')\n",
    "    # 点击滑块,并且hold住\n",
    "    action.click_and_hold(on_element=e_nc).perform()\n",
    "    x = 5\n",
    "    y = 1\n",
    "    for i in range(70):\n",
    "        x += 5\n",
    "        if i <= 35:\n",
    "            y += 1\n",
    "            action.move_to_element_with_offset(e_nc,x,y)\n",
    "        else:\n",
    "            y -= 1\n",
    "            action.move_to_element_with_offset(e_nc,x,y)\n",
    "    try:\n",
    "        action.perform()\n",
    "        action.release(e_nc)\n",
    "    except:\n",
    "        pass\n",
    "\n",
    "def login():\n",
    "    '''登录'''\n",
    "    url = 'https://login.taobao.com/member/login.jhtml?style=mini&from=alimama'\n",
    "    browser.get(url)\n",
    "    browser.find_element_by_xpath('//input[@name=\"TPL_username\"]').send_keys(u'')\n",
    "    browser.find_element_by_xpath('//input[@name=\"TPL_password\"]').send_keys('')\n",
    "    sleep(1)\n",
    "    browser.find_element_by_xpath('//input[@name=\"TPL_password\"]').click()\n",
    "    browser.find_element_by_xpath('//input[@name=\"TPL_username\"]').click()\n",
    "    # 滑块操作\n",
    "    slider()\n",
    "    browser.find_element_by_id('J_SubmitStatic').click()\n",
    "    sleep(3)\n",
    "#     print browser.get_cookies() \n",
    "\n",
    "def rewrite_cookie(browser, cookies):\n",
    "    '''重写cookie'''\n",
    "    browser.delete_all_cookies()\n",
    "    for cookie in cookies:\n",
    "        browser.add_cookie(cookie)\n",
    "    return browser\n",
    "\n",
    "login()\n",
    "a = 1\n",
    "while a <= 10:\n",
    "    a += 1\n",
    "    print a\n",
    "    sleep(3)\n",
    "    if browser.current_url.find('login.taobao.com') != -1:\n",
    "        print browser.current_url\n",
    "#         br.close()\n",
    "        browser.find_element_by_xpath('//input[@name=\"TPL_username\"]').clear()\n",
    "        sleep(1)\n",
    "#         browser = webdriver.Firefox()\n",
    "        browser.find_element_by_xpath('//input[@name=\"TPL_username\"]').send_keys(u'')\n",
    "        browser.find_element_by_xpath('//input[@name=\"TPL_password\"]').send_keys('')\n",
    "        sleep(1)\n",
    "        browser.find_element_by_xpath('//input[@name=\"TPL_password\"]').click()\n",
    "        browser.find_element_by_xpath('//input[@name=\"TPL_username\"]').click()\n",
    "        sleep(300)\n",
    "        # 滑块操作\n",
    "        slider(br)\n",
    "        sleep(3)\n",
    "        browser.find_element_by_id('J_SubmitStatic').click()\n",
    "        sleep(3)\n",
    "    else:\n",
    "        cookies = browser.get_cookies()\n",
    "        if browser:\n",
    "            browser.close()\n",
    "        br = webdriver.Firefox()\n",
    "        url = 'http://www.baidu.com'\n",
    "        br.get(url)\n",
    "        br = rewrite_cookie(br,cookies)\n",
    "        sleep(3)\n",
    "        url = 'https://market.m.taobao.com/apps/aliyx/coupon/detail.html?wh_weex=true&activity_id=0ad0e948a95848589bb386c039255e17&seller_id=3091520751'\n",
    "#         url = 'http://shop.m.taobao.com/shop/coupon.htm?sellerId=859450200&activityId=14a3d85edb06435ca81000318b55738d'\n",
    "        \n",
    "        br.get(url)\n",
    "        print br.get_cookies()\n",
    "        break\n",
    "# br.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
